package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;

import com.jiayen.cache.FileCacheManager;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.dialog.CommDialog;
import com.jp.knowledge.dialog.SetFontDialog;
import com.jp.knowledge.fragment.MyFragment;
import com.jp.knowledge.logic.PersonLogic;
import com.jp.knowledge.model.UserData;
import com.jp.knowledge.my.util.MyUtil;
import com.jp.knowledge.my.view.RoundRectView;
import com.jp.knowledge.util.DataCleanManager;
import com.jp.knowledge.util.PackInfo;
import com.jp.knowledge.util.SharedUtil;

import org.xutils.view.annotation.ViewInject;

/**
 * 设置
 *
 * @author gzejia 978862664@qq.com
 */
public class SetActivity extends SlidingActivity implements View.OnClickListener {

    @ViewInject(R.id.set_tv_font)
    private TextView mFontSizeTv;

    @ViewInject(R.id.set_tv_clean)
    private TextView mCacheSizeTv;

    @ViewInject(R.id.set_tv_disturb)
    private TextView mDisturbTv;

    @ViewInject(R.id.set_sw_night)
    private Switch mNightSw;

    @ViewInject(R.id.set_sw_disturb)
    private Switch mDisturbSw;

    @ViewInject(R.id.set_sw_message)
    private SwitchCompat mMessageSw;

    @ViewInject(R.id.set_sw_economy)
    private Switch mEconomySw;

    @ViewInject(R.id.set_sw_sound)
    private Switch mSoundSw;

    @ViewInject(R.id.set_sw_shake)
    private Switch mShakeSw;

    @ViewInject(R.id.set_v_version)
    private RoundRectView mVersionRrv;


    @ViewInject(R.id.set_btn_logout)
    private TextView   exitBtn;

    private CommDialog commDialog;
    private CommDialog clearCacheDialog;

    private SetFontDialog setFontDialog;

    private UserData userData;
    @Override
    protected int getResourceId() {
        return R.layout.activity_set;
    }

    @Override
    protected void init() {
        initExitDialog();
        initCache();
        initOthers();
        initTitle();

        initClickListener();

        userData =application.getUserData();
        if(userData==null){
            exitBtn.setVisibility(View.GONE);
        }else{
            exitBtn.setVisibility(View.VISIBLE);
        }
    }

    private String cachePath;
    private FileCacheManager cacheManager;
    private void initCache(){
        cachePath = getCacheDir().getPath() + "/userData.dat";
        cacheManager = new FileCacheManager();
    }

    private void saveUserDataToCache(final  UserData userData){
        new Thread(new Runnable() {
            @Override
            public void run() {
                cacheManager.wirteObject(userData, cachePath);
            }
        }).start();
    }

    private void initExitDialog(){
       String  text =  SharedUtil.getInstance(mContext).getCommFontSizeText();
        if(text==null)mFontSizeTv.setText("中");
        else mFontSizeTv.setText(text);

        setFontDialog = new SetFontDialog(mContext,mFontSizeTv);
        commDialog  = new CommDialog(mContext);
        commDialog.setContent("你即将退出当前账号?");
        commDialog.setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                application.clearLoginInfo();
                application.setUserData(null);
//                saveUserDataToCache(new UserData());
                new PersonLogic(mContext).deleteUserData();
                // 发送退出登陆广播
                LocalBroadcastManager.getInstance(mContext)
                        .sendBroadcast(new Intent(LocalBroadCast.LOGOUT_SUCCESS_BROD));
                application.getApplicationWebView().setUserDataToH5(null);

                SetActivity.this.setResult(MyFragment.EXIT_LOGIN_CODE);
                SetActivity.this.finish();
            }
        });


        clearCacheDialog = new CommDialog(this);
        clearCacheDialog.setContent("是否清空所有缓存数据？");
        clearCacheDialog.setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataCleanManager.cleanApplicationData(mContext);
                mCacheSizeTv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            mCacheSizeTv.setText(DataCleanManager.getCacheSize(getCacheDir()));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, 500);
            }
        });
    }

    private void initClickListener(){
        findViewById(R.id.icon_left).setOnClickListener(this);
        findViewById(R.id.set_lly_font).setOnClickListener(this);
        findViewById(R.id.set_lly_clean).setOnClickListener(this);
        findViewById(R.id.set_lly_score).setOnClickListener(this);
        findViewById(R.id.set_lly_update).setOnClickListener(this);
        findViewById(R.id.set_btn_logout).setOnClickListener(this);
        findViewById(R.id.set_tv_font).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.icon_left:// 返回
                break;
            case R.id.set_lly_font:// 字体大小
                break;
            case R.id.set_lly_clean:// 清理缓存
                clearCacheDialog.show();
                break;
            case R.id.set_lly_score:// 去App Store评分
                break;
            case R.id.set_lly_update:// 检查新版本
                break;
            case R.id.set_btn_logout:// 退出账号
                commDialog.show();
                break;
            case R.id.set_tv_font://设置字体大小
                setFontDialog.show();
                break;
            default:
                break;
        }
    }

    private void initTitle() {
        topName.setText(R.string.set_tv_title);
        topDesc.setVisibility(View.GONE);
        leftIcon.setVisibility(View.VISIBLE);
        rightIcon.setVisibility(View.GONE);
    }

    private void initOthers() {
        String disturbStr = getResources().getString(R.string.set_tv_disturb);
        mDisturbTv.setText(MyUtil.changeTextColor(disturbStr, R.color.gray_light,
                disturbStr.indexOf("（"), disturbStr.indexOf("）") + 1));

        /* 版本信息 */
        String versionStr = PackInfo.getVersionName(mContext)
                + " (" + getSharedPreferences("H5VersionData", MODE_PRIVATE).getLong("latest", 0) + ")";
        ((TextView)findViewById(R.id.set_tv_update)).setText(versionStr);

        /* 缓存信息 */
        try {
            mCacheSizeTv.setText(DataCleanManager.getCacheSize(getCacheDir()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
